package com.itheima.project.knowledge;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.itheima.project.*;

import java.util.List;

public class RvNavigationAdapter extends RecyclerView.Adapter<RvNavigationAdapter.RvNavigationHolder> {

    private List<KnowledgeTreeEntity.DataBean.ChildrenBean> mList;

    private RecyclerView recyclerView;

    private int mSelectPosition = 0;

    public RvNavigationAdapter(List<KnowledgeTreeEntity.DataBean.ChildrenBean> mList, RecyclerView recyclerView) {
        this.mList = mList;
        this.recyclerView = recyclerView;
    }

    @NonNull
    @Override
    public RvNavigationHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.knowledge_rvnavigation_adapter, parent, false);
        return new RvNavigationHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final RvNavigationHolder holder, int position) {
        if (mSelectPosition == position) {
            holder.tvContent.setSelected(true);
        }else {
            holder.tvContent.setSelected(false);
        }
        final KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean = mList.get(position);
        holder.tvContent.setText(childrenBean.getName());

        if (mListener != null) {
            holder.tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int adapterPosition = holder.getAdapterPosition();
                    mListener.onIndicatorItemClick(
                            adapterPosition,
                            childrenBean);
                    //了解即可，不需掌握
//                    scrollToCenter(holder.getAdapterPosition());

                    holder.tvContent.setSelected(true);

                    mSelectPosition = adapterPosition;
                }
            });
        }
    }
    public void setCurrentItem(int i){
        mSelectPosition = i;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class RvNavigationHolder extends RecyclerView.ViewHolder{

        TextView tvContent;

        public RvNavigationHolder(@NonNull View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content_knowledge_navigation_adapter);
        }
    }

    private onIndicatorItemClickListener mListener;

    public void setOnIndicatorItemClickListener(onIndicatorItemClickListener listener) {
        this.mListener = listener;
    }

    public interface onIndicatorItemClickListener {

        void onIndicatorItemClick(
                int position,
                KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean);
    }
}
